﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EventReminder.Common;

namespace EventReminder.Client
{
    internal partial class EventForm : Form
    {
        private EventReminderController _controller;

        public DateTime Date { get; set; }

        public EventForm()
            : this (null)
        { }

        public EventForm(EventReminderController eventReminderController)
        {
            // hook up to any Model events?  Maybe expect verification that save/update was completed okay...
            this._controller = eventReminderController;

            _controller.EventAdded += new EventHandler(HandleBtnQuit_Click);
            _controller.EventModified += new EventHandler(HandleBtnQuit_Click);

            InitializeComponent();
        }

        private void HandleBtnAdd_Click(object sender, EventArgs e)
        {
            // have controller...
            // validate email format
            // add email to list box
            // clear email text field
        }

        private void HandleBtnRemove_Click(object sender, EventArgs e)
        {
            // remove email address
        }

        private void HandleBtnAccept_Click(object sender, EventArgs e)
        {
            // create date
            DateTime date = dtpDate.Value;
            DateTime time = dtpTime.Value;
            DateTime dateTime = new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second);

            // create list of contents
            List<string> emails = new List<string>();
            foreach (object o in lstEmails.Items)
            {
                emails.Add(o.ToString());
            }

            _controller.AcceptEvent(txtTitle.Text,
                txtDescr.Text,
                date,
                cbType.SelectedValue,
                txtReminder.Text,
                emails);
        }

        private void HandleBtnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override void OnClosed(EventArgs e)
        {
            // observer unsubscribe

            base.OnClosed(e);
        }
    }
}
